<%@ page pageEncoding="UTF-8"%>
<%@page import="itsm.isperp.module.entity.app.AppRole"%>
<%@page import="java.util.List"%>
<%@page import="itsm.isperp.framework.core.context.ContextHolder"%>
<%@page import="itsm.isperp.module.service.app.AppRoleService"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<style type="text/css">
.ui-jqgrid-resize-mark {
	display: none
}

.ui-jqgrid-bdiv tr.ui-row-ltr {
	height: 30px;
}
</style>
<script type="text/javascript">
	function userTree_onNodeClick(event, treeId, treeNode) {
		userListSelectorGrid.resetSearchFilters();
		userListSelectorGrid.search({
			"field" : "department_treenode_path",
			"op" : "bw",
			"data" : treeNode.treenodePath
		});
	}
</script>
<table class="" style="width:96%;padding: 0;margin:1%">

	<tr>
		<td style="">
			<div class="department-user-table" style="border:node;height:100%">

				<div
					style="width:159px;vertical-align: top;float:left;border-right:1px solid #eee">

					<div id="accordion">
						<h3>按部门</h3>
						<div>
							<isperp:tree id="userTree" cssStyle="height:200px;overflow:auto">
								<isperp:async enable="true" autoParam="'id'"
									url="app/department/getTree">
								</isperp:async>
								<isperp:callback onClick="userTree_onNodeClick">
								</isperp:callback>
							</isperp:tree>
						</div>
						<h3>按角色</h3>
						<div style="height:200px;overflow:auto">
							<%
								AppRoleService appRoleService = ContextHolder.getSpringBean("appRoleService");

																	List<AppRole> roles = appRoleService.findAll();

																	for (AppRole role : roles) {
							%>
							<p class="nui-txt-link" data-id="<%=role.getId()%>"><%=role.getName()%></p>

							<%
								}
							%>

						</div>
					</div>
				</div>
				<div style="vertical-align: top;width:306px;float:left">
					<isperp:grid postData="userNames:'${userNames }'" mtype="POST"
						url="app/user/deptmentSysUser?rolenames=&ifIncludeNoRoleUser=&projectIdForPrjMember="
						autoload="false" filterToolbar="true" showSearchbar="true"
						showToolbar="false" multikey="true" rowNum="5" rownumbers="false"
						idFieldName="name" gridId="userListSelectorGrid"
						columnFilter="false" height="200" pager="true"
						cusFormatterActions="false"
						onSelectRow="onSelectRowEvent(rowid,status);"
						afterGridCreate="userListSelectorGrid.search()"
						ondbClickRowEvent="ondbClickRowEventEvent();">
						<jsp:attribute name="searchbar">
				<table class="search-table" style="display: none">
					<tbody>
						<tr>
							<td>
							    <!-- <input type="hidden" field="name" op="isnotnull" value="1"> -->
							</td>
						</tr>
					</tbody>
				</table>
			
			</jsp:attribute>
						<jsp:body>	
			<isperp:column name="id" title="id" hidden="true" width="10"></isperp:column>
			<isperp:column name="name" title="帐号" width="110">
			</isperp:column>
			<isperp:column name="fullname" title="姓名" width="100">
			</isperp:column>
			<isperp:column name="position" title="职位" width="90">
			</isperp:column>
			<isperp:column name="app_department_id" hidden="true"
								title="appDepartmentId">
			</isperp:column>
			<isperp:column name="department_treenode_path" hidden="true"
								title="departmentTreenodeNamePath">
			</isperp:column>
			</jsp:body>
					</isperp:grid>

				</div>

				<div
					style="vertical-align:top;float:left;width:65px;overflow: hidden">
					<div style="padding:10px 0px 0px 5px" id="selectedRows"></div>
				</div>
			</div>
		</td>
	</tr>

	<tr>
		<td style="padding:8px">
			<div class="row no-bg">
				<p class="_100">
					<label for="" style="font-weight: bold;">处理意见</label>
					<textarea id="suggestion" name="suggestion" rows="3"
						style="width:96%" class="${suggestionRequired }"></textarea>
				</p>
			</div>
		</td>
	</tr>
	<tr>
		<td style="padding:8px"><div class="row">
				<p class="_100 small">
					<label for="" style="font-weight: bold;">常用意见</label> <select
						data-width="100%" onchange="appendToSuggestion(this)">
						<option value="" selected="selected"></option>
						<c:forEach items="${views }" var="item">
							<option value="${item.name }">${item.name }</option>
						</c:forEach>
					</select>
				</p>
			</div>
		</td>
	</tr>

</table>

<script type="text/javascript">
	function appendToSuggestion(obj) {
		var sug = $("#suggestion");
		sug.val(sug.val() + $(obj).val());
	}

	function submitProcess() {

		var $sug = $("#suggestion").val();
		/* if (($sug == "" || $sug == undefined)) {
			isperp.alert("请填写处理意见");
			return false;
		} */

		var processMode = "${processMode}";
		var commitType = "${commitType}";
		if (processMode == "0" && commitType != "0") {
			var uname = "";

			if (commitType == "1") {
				uname = $("#username").val();
			} else if (commitType == "2" || commitType == "3") {
				/* $("input[name=username]:checked").each(function(index, item) {
					if (index != 0) {
						uname += ",";
					}
					uname += $(item).val();
				}); */
				uname = getSelectedValue();
			}
			if (uname == "") {
				isperp.errorAlert("请选择处理人");
				return;
			}
		}

		//$("#approvelBtns").hide();
		//$("#approvelBtns").parent().append("<label style='float:right;font-size:14px;color:red;'>正在提交，请稍后......</lable>");

		$.post("workflow/processRun/gotoNextStep", {
			"key" : "${processKey}",
			"nextTaskId" : "${nextTaskId}",
			"nextTransitionId" : "${nextTransitionId}",
			"processId" : "${processId}",
			"name" : "${name}",
			"businessKey" : "${businessKey}",
			"username" : uname,
			"processMode" : "${processMode}",
			"commitType" : "${commitType}",
			"fromId" : "${fromId}",
			"fromProcessKey" : "${fromProcessKey}",
			"suggestion" : $sug
		}, function(result) {
			if (result.success) {
				isperp.alert(result.data);
				isperp.reloadParentControlById();
				isperp.closeDialog({
					closeParent : true
				});

			}

		});

	}
</script>
<script type="text/javascript">
	$("#accordion").accordion({
		autoHeight : false,
		heightStyle : "content"
	});

	$(".nui-txt-link").click(function() {
		$(".nui-txt-link").removeClass("active");
		$(this).addClass("active");
		userListSelectorGrid.resetSearchFilters();
		userListSelectorGrid.search({
			"field" : "name",
			"op" : "in",
			"data" : " (SELECT app_user_name FROM app_user_role WHERE app_role_id ='" + $(this).data("id") + "')"
		});
	});

	function onSelectRowEvent(rowid, status) {
		if (!status) {
			$("#selectedRows").find("#d_" + rowid).remove();
			return;
		}
		var rowData = userListSelectorGrid.getRowData(rowid);
		var value = rowData.name;
		var find = false;
		$("#selectedRows .box").each(function(index, item) {
			if (value == $(this).data("value")) {
				find = true;
				return;
			}
		});
		if (find) {
			return false;
		}
		var html = '<div class="box" style="padding:4px;margin-top:3px" data-value="'+
			value + '" data-name="'+rowData.fullname+'" id="d_'+rowData.id+'">';
		html += '<a href="javascript:void(0);" onclick="deleteSelected(\'' + rowData.id + '\')"><span class="icon icon-remove "></span>' + rowData.fullname + '</div>';
		if (1 == 2) {
			$("#selectedRows").html(html);
		} else {
			$("#selectedRows").append(html);
		}
	}

	function ondbClickRowEventEvent() {
		if (1 == 1) {
			var rowData = userListSelectorGrid.getRowData();
			var value = rowData.name;
			window.parent.userSetSelected([ value, rowData.fullname, rowData ]);
		}
	}

	function deleteSelected(id) {
		userListSelectorGrid.jqGrid.setSelection(id, false);
		$("#selectedRows").find("#d_" + id).remove();
	}

	function getSelectedValue() {
		var values, names;
		$("#selectedRows .box").each(function(index, item) {
			if (index == 0) {
				values = $(this).data("value");
				names = $(this).data("name");
			} else {
				values += "," + $(this).data("value");
				names += "," + $(this).data("name");
			}
		});
		return values;
	}

	function getSelectedRowValue() {
		var values, names;
		$("#selectedRows .box").each(function(index, item) {
			if (index == 0) {
				values = $(this).data("value");
				names = $(this).data("name");
			} else {
				values += "," + $(this).data("value");
				names += "," + $(this).data("name");
			}
		});
		return [ values, names, userListSelectorGrid.getRowData() ];
	}
</script>

